﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace HOG.Behaviors
{
    public class SceneHide : Behavior<FrameworkElement>
    {
        private Storyboard myStoryboard;

        public SceneHide()
        {
            myStoryboard = new Storyboard();
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            Invoke();
        }

        protected void Invoke()
        {
            myStoryboard.Seek(new TimeSpan(0, 0, 0, 0, 0));
            if (myStoryboard.GetCurrentState() == ClockState.Stopped)
            {
                myStoryboard.Duration = new TimeSpan(0, 0, 0, 0, 600);
                DoubleAnimation OpacityAnim = new DoubleAnimation();
                OpacityAnim.From = 1.0;
                OpacityAnim.To = 0.0;
                OpacityAnim.Duration = new TimeSpan(0,0,0,0,500);
                myStoryboard.Children.Add(OpacityAnim);
                Storyboard.SetTarget(OpacityAnim, AssociatedObject);
                Storyboard.SetTargetProperty(OpacityAnim, new PropertyPath("(Opacity)"));

                DoubleAnimation XScaleAnim = new DoubleAnimation();
                XScaleAnim.From= 1.0;
                XScaleAnim.To = 0.9;
                XScaleAnim.Duration = new TimeSpan(0, 0, 0, 0, 500); 
                myStoryboard.Children.Add(XScaleAnim);
                ScaleTransform scaleTransform = new ScaleTransform();
                scaleTransform.CenterX = (double)AssociatedObject.GetValue(FrameworkElement.ActualWidthProperty) / 2;
                scaleTransform.CenterY = (double)AssociatedObject.GetValue(FrameworkElement.ActualHeightProperty) / 2;

                AssociatedObject.RenderTransform = scaleTransform;
                Storyboard.SetTarget(XScaleAnim, AssociatedObject);
                Storyboard.SetTargetProperty(XScaleAnim, new PropertyPath("(FrameworkElement.RenderTransform).(ScaleTransform.ScaleX)"));

                DoubleAnimation YScaleAnim = new DoubleAnimation();
                YScaleAnim.From = 1.0;
                YScaleAnim.To = 0.9;
                YScaleAnim.Duration = new TimeSpan(0, 0, 0, 0, 500); 
                myStoryboard.Children.Add(YScaleAnim);
                Storyboard.SetTarget(YScaleAnim, AssociatedObject);
                Storyboard.SetTargetProperty(YScaleAnim, new PropertyPath("(FrameworkElement.RenderTransform).(ScaleTransform.ScaleY)"));
                myStoryboard.Begin();
                
            }
            

            
        }
    }

}
